package com.xc.file.web.rest;

import com.xc.core.annotation.Authority;
import com.xc.core.bean.QueryBean;
import com.xc.core.model.TokenModel;
import com.xc.file.bean.FileBean;
import com.xc.file.bean.FolderBean;
import com.xc.file.bean.FolderFileBean;
import com.xc.file.dto.FileDto;
import com.xc.file.dto.FolderDto;
import com.xc.file.dto.FolderFileDto;
import com.xc.file.service.FileService;
import com.xc.file.service.FolderService;
import com.xc.file.service.ShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>文件夹及文件rest</p>
 *
 * @author xc
 * @version v1.0.0
 */
@Api(tags = "文件夹及文件rest")
@RestController
public class FolderFileRest {
    @Autowired
    private FolderService folderService;
    @Autowired
    private FileService fileService;
    @Autowired
    private ShareService shareService;


    @ApiOperation(value = "获取共享的文件夹及文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "共享id", name = "shareId", paramType = "path", required = true),
            @ApiImplicitParam(value = "文件夹id", name = "folderId", paramType = "query"),
    })
    @GetMapping("/share/{shareId}/folder_files")
    public FolderFileDto getShareFolderFile(@PathVariable String shareId,
                                            @RequestParam(required = false) String folderId,
                                            @RequestParam(required = false) String name,
                                            @ModelAttribute QueryBean queryBean) {
        return shareService.getShareFolderFile(shareId, folderId, name, queryBean);
    }


    @ApiOperation(value = "获取当前用户的文件夹及文件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件夹id", name = "folderId", paramType = "query"),
            @ApiImplicitParam(value = "名称", name = "name", paramType = "query"),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @GetMapping("/current_user/folder_files")
    @Authority
    public FolderFileDto getCurrentUserFolderFile(TokenModel tokenModel, @RequestParam(required = false) String folderId,
                                                  @RequestParam(required = false) String name, @ModelAttribute QueryBean queryBean) {
        FolderFileDto folderFileDto = new FolderFileDto();
        FileBean fileBean = new FileBean();
        fileBean.setFolderId(folderId);
        fileBean.setName(name);
        folderFileDto.setFiles(fileService.getUserFileList(tokenModel.getUserId(), queryBean, fileBean));
        FolderBean folderBean = new FolderBean();
        folderBean.setParentId(folderId);
        folderBean.setName(name);
        folderFileDto.setFolders(folderService.getUserFolderList(tokenModel.getUserId(), queryBean, folderBean));
        return folderFileDto;
    }


    @ApiOperation(value = "创建当前用户的文件夹及文件(复制文件夹及文件/保存共享文件夹及文件)",
            notes = "1，shareId为空，批量复制文件夹及文件\n" +
                    "2，shareId不为空，保存共享文件及文件夹")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "新目录id", name = "folderId", paramType = "query", required = true),
            @ApiImplicitParam(value = "共享id", name = "shareId", paramType = "query"),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
    })
    @PostMapping("/current_user/folder_file")
    @Authority
    public FolderFileDto createCurrentUserFolderFile(TokenModel tokenModel, @RequestParam String folderId,
                                                     @RequestParam(required = false) String shareId, @RequestBody FolderFileBean folderFileBean) {
        //验证目录信息
        folderId = folderService.verifyFolder(folderId, tokenModel.getUserId());
        if (shareId == null) {
            return folderService.batchCopyFolderFile(tokenModel.getUserId(), folderId, folderFileBean);
        } else {
            return folderService.saveShareFolderFile(tokenModel.getUserId(), folderId, shareId, folderFileBean);
        }
    }

    @ApiOperation(value = "修改当前用户的文件夹及文件(批量移动文件夹及文件)")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "目录id", name = "folderId", paramType = "path", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
    })
    @PutMapping("/current_user/folder_file")
    @Authority
    public FolderFileDto updateCurrentUserFolderFile(TokenModel tokenModel, @RequestParam String folderId, @RequestBody FolderFileBean folderFileBean) {
        //验证目录信息
        folderId = folderService.verifyFolder(folderId, tokenModel.getUserId());
        //移动文件及目录
        List<FileDto> files = fileService.updateUserFileList(tokenModel.getUserId(), folderId, folderFileBean.getFileIds());
        List<FolderDto> folders = folderService.updateUserFolderList(tokenModel.getUserId(), folderId, folderFileBean.getFolderIds());
        FolderFileDto folderFileDto = new FolderFileDto();
        folderFileDto.setFiles(files);
        folderFileDto.setFolders(folders);
        return folderFileDto;
    }
}
